package com.pro.addresslist.ui.view;


import com.pro.addresslist.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;


public class CircleView extends View {

	private Paint mPaintCircle;
    private Paint mPaintText;
    //圆
    private int mWidth;
    private int mHeight;
    private float mRadius;
    private int fillColor;
    //文字
    private String mTextStr="无";
    private Rect mBoundText;
    private int mTextWidth;
    private int mTextHeight;
    private int textColor;
    private int textSize;

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
    	
    	TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Circle);
//        int count = ta.getIndexCount();
//        for(int i=0; i<count; i++) {
//            int attr = ta.getIndex(i);
//            switch(attr) {
//                case R.styleable.Circle_text:
//                	mTextStr = ta.getString(attr);
//                    break;
//                case R.styleable.Circle_textSize:
//                    textSize = ta.getInt(attr,40);
//                    break;
//                case R.styleable.Circle_textColor:
//                    textColor = ta.getColor(attr, R.color.white);
//                    break;
//                case R.styleable.Circle_fillColor:
//                    fillColor = ta.getColor(attr, android.R.color.holo_orange_dark);
//                    break;
//                default:
//                    break;
//            }
//        }
//        ta.recycle();
    	if (ta != null) {
    		mTextStr = ta.getString(R.styleable.Circle_text);
    		textSize = ta.getInt(R.styleable.Circle_textSize, 40);
    		textColor = ta.getColor(R.styleable.Circle_textColor, R.color.color_white);
    		fillColor = ta.getColor(R.styleable.Circle_fillColor, R.color.color_theme);
            ta.recycle();
        }

        mPaintCircle = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintCircle.setColor(fillColor);
        
        mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintText.setColor(textColor);
        mPaintText.setTextSize(textSize);
        mBoundText = new Rect();
        mPaintText.getTextBounds(mTextStr, 0, mTextStr.length(), mBoundText);
        mTextWidth = mBoundText.width();
        mTextHeight = mBoundText.height();
    }
    
    

    protected void onSizeChanged (int w, int h, int oldw, int oldh) {
        mWidth = w;  // x 的值就是宽度的一半
        mHeight = h;  // y 的值就是高度的一半
        mRadius = Math.min(mWidth / 2, mHeight / 2);  // 得到了radius的值
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mPaintCircle);
        canvas.drawText(mTextStr, (mWidth-mTextWidth) / 2, (mHeight + mTextHeight) / 2, mPaintText);
    }
    
}
